#include "CAnimal.h"

CAnimal::CAnimal(void) {
	this->direction = true;
	this->width = 0;
	this->height = 0;
	this->img = NULL;
}

CAnimal::~CAnimal(void) {

}

double CAnimal::GetSecondTime() {
	time_t timer;
	struct tm y2k;
	double seconds;
	y2k.tm_hour = 0;   y2k.tm_min = 0; y2k.tm_sec = 0;
	y2k.tm_year = 114; y2k.tm_mon = 0; y2k.tm_mday = 1;
	time(&timer);  /* get current time; same as: timer = time(NULL)  */
	seconds = difftime(timer,mktime(&y2k));
	return seconds;
}

void CAnimal::Init(CContent *content,D3DXVECTOR2 pos,string path) {
	this->img = content->LoadTexture(path.c_str());
	this->position = pos;
	this->speed = D3DXVECTOR2(5,0);
	this->height = 88;
	this->width = 82;
	this->direction = true;
	this->startTime = GetSecondTime();
	
	/*
	this->rect.left = (LONG)this->position.x;
	this->rect.top = (LONG)this->position.y;
	this->rect.right = (LONG)this->position.x + this->width;
	this->rect.bottom = (LONG)this->position.y + this->height;
	*/
	this->rect.left = 0;
	this->rect.top = 0;
	this->rect.right = 82;
	this->rect.bottom = 88;
	this->center = D3DXVECTOR3(0,0,0);
}

void CAnimal::Update(float gameTime) {
	this->curTime = GetSecondTime();
	if(this->direction) {
		this->position += this->speed;
	} else {
		this->position -= this->speed;
	}
	if(this->position.x > 800 - this->width) {
		this->direction = false;
	} else if (this->position.x < 0) {
		this->direction = true;
	}
}

void CAnimal::Render(CGraphic *graphic) {
	graphic->DrawTexture(this->img,this->center,this->position,D3DCOLOR_XRGB(255,255,255));
}

void CAnimal::Shutdown() {
	if(this->img) {
		this->img->Release();
	}
}

bool CAnimal::CheckTimeToLayingEgg(void) {
	unsigned long dif = this->curTime - this->startTime;
	return ((((dif % 5) == 0) && (dif > 0)) ? true : false);
}
